﻿using System.Windows.Controls;
using MainGameModule.ViewModels;

namespace MainGameModule.Views
{
    public partial class QuizView : UserControl
    {
        private QuizViewModel viewModel;

        public QuizView(QuizViewModel vm)
        {
            InitializeComponent();

            this.viewModel = vm;
            this.DataContext = this.viewModel;
            
            this.Answer1.Loaded += (s, e) =>
            {
                this.Answer1.IsChecked = true;
            };
        }

        private void ShowAnswerButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            string selectedAnswer = string.Empty;
            if (this.Answer1.IsChecked.HasValue && this.Answer1.IsChecked == true)
            {
                selectedAnswer = this.Answer1.Content as string;
            }
            else if (this.Answer2.IsChecked.HasValue && this.Answer2.IsChecked == true)
            {
                selectedAnswer = this.Answer2.Content as string;
            }
            else if (this.Answer3.IsChecked.HasValue && this.Answer3.IsChecked == true)
            {
                selectedAnswer = this.Answer3.Content as string;
            }
            else
            {
                selectedAnswer = this.Answer4.Content as string;
            }

            if (string.Compare(selectedAnswer, this.viewModel.CurrentQuestion.CorrectAnswer) == 0)
            {
                this.ValidityText.Text = "Correct answer!";
                this.viewModel.NumberOfCorrectAnswers++;
            }
            else
            {
                this.ValidityText.Text = "Wrong answer! Correct answer is '" + this.viewModel.CurrentQuestion.CorrectAnswer + "'.";
            }
            this.ShowAnswerButton.Visibility = System.Windows.Visibility.Collapsed;
            if (this.viewModel.CurrentQuestionNumber == this.viewModel.NumberOfQuestions)
            {
                this.FinishButton.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                this.NextQuestionButton.Visibility = System.Windows.Visibility.Visible;
            }
        }

        private void NextQuestionButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            this.ValidityText.Text = string.Empty;
            this.NextQuestionButton.Visibility = System.Windows.Visibility.Collapsed;
            this.ShowAnswerButton.Visibility = System.Windows.Visibility.Visible;
        }

        private void FinishButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            this.QuestionsHolder.Visibility = System.Windows.Visibility.Collapsed;
            this.OverviewHolder.Visibility = System.Windows.Visibility.Visible;
        }
    }
}
